<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>link_test.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_editor_link_test.js.html">link_test.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2008 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>goog.provide(&#39;goog.editor.LinkTest&#39;);
<a name="line16"></a>goog.setTestOnly(&#39;goog.editor.LinkTest&#39;);
<a name="line17"></a>
<a name="line18"></a>goog.require(&#39;goog.dom&#39;);
<a name="line19"></a>goog.require(&#39;goog.dom.NodeType&#39;);
<a name="line20"></a>goog.require(&#39;goog.dom.Range&#39;);
<a name="line21"></a>goog.require(&#39;goog.dom.TagName&#39;);
<a name="line22"></a>goog.require(&#39;goog.editor.Link&#39;);
<a name="line23"></a>goog.require(&#39;goog.testing.jsunit&#39;);
<a name="line24"></a>goog.require(&#39;goog.userAgent&#39;);
<a name="line25"></a>
<a name="line26"></a>var anchor;
<a name="line27"></a>
<a name="line28"></a>function setUp() {
<a name="line29"></a>  anchor = goog.dom.createDom(&#39;A&#39;);
<a name="line30"></a>  document.body.appendChild(anchor);
<a name="line31"></a>}
<a name="line32"></a>
<a name="line33"></a>function tearDown() {
<a name="line34"></a>  goog.dom.removeNode(anchor);
<a name="line35"></a>}
<a name="line36"></a>
<a name="line37"></a>function testCreateNew() {
<a name="line38"></a>  var link = new goog.editor.Link(anchor, true);
<a name="line39"></a>  assertNotNull(&#39;Should have created object&#39;, link);
<a name="line40"></a>  assertTrue(&#39;Should be new&#39;, link.isNew());
<a name="line41"></a>  assertEquals(&#39;Should have correct anchor&#39;, anchor, link.getAnchor());
<a name="line42"></a>  assertEquals(&#39;Should be empty&#39;, &#39;&#39;, link.getCurrentText());
<a name="line43"></a>}
<a name="line44"></a>
<a name="line45"></a>function testCreateNotNew() {
<a name="line46"></a>  var link = new goog.editor.Link(anchor, false);
<a name="line47"></a>  assertNotNull(&#39;Should have created object&#39;, link);
<a name="line48"></a>  assertFalse(&#39;Should not be new&#39;, link.isNew());
<a name="line49"></a>  assertEquals(&#39;Should have correct anchor&#39;, anchor, link.getAnchor());
<a name="line50"></a>  assertEquals(&#39;Should be empty&#39;, &#39;&#39;, link.getCurrentText());
<a name="line51"></a>}
<a name="line52"></a>
<a name="line53"></a>function testInitialize() {
<a name="line54"></a>  var link = goog.editor.Link.createNewLink(anchor, &#39;http://www.google.com&#39;);
<a name="line55"></a>  assertNotNull(&#39;Should have created object&#39;, link);
<a name="line56"></a>  assertTrue(&#39;Should be new&#39;, link.isNew());
<a name="line57"></a>  assertEquals(&#39;Should have correct anchor&#39;, anchor, link.getAnchor());
<a name="line58"></a>  assertEquals(&#39;Should be empty&#39;, &#39;&#39;, link.getCurrentText());
<a name="line59"></a>}
<a name="line60"></a>
<a name="line61"></a>function testInitializeWithTarget() {
<a name="line62"></a>  var link = goog.editor.Link.createNewLink(anchor, &#39;http://www.google.com&#39;,
<a name="line63"></a>      &#39;_blank&#39;);
<a name="line64"></a>  assertNotNull(&#39;Should have created object&#39;, link);
<a name="line65"></a>  assertTrue(&#39;Should be new&#39;, link.isNew());
<a name="line66"></a>  assertEquals(&#39;Should have correct anchor&#39;, anchor, link.getAnchor());
<a name="line67"></a>  assertEquals(&#39;Should be empty&#39;, &#39;&#39;, link.getCurrentText());
<a name="line68"></a>  assertEquals(&#39;Should have _blank target&#39;, &#39;_blank&#39;, anchor.target);
<a name="line69"></a>}
<a name="line70"></a>
<a name="line71"></a>function testSetText() {
<a name="line72"></a>  var link = goog.editor.Link.createNewLink(anchor, &#39;http://www.google.com&#39;,
<a name="line73"></a>      &#39;_blank&#39;);
<a name="line74"></a>  assertEquals(&#39;Should be empty&#39;, &#39;&#39;, link.getCurrentText());
<a name="line75"></a>  link.setTextAndUrl(&#39;Text&#39;, &#39;http://docs.google.com/&#39;);
<a name="line76"></a>  assertEquals(&#39;Should point to http://docs.google.com/&#39;,
<a name="line77"></a>      &#39;http://docs.google.com/&#39;, anchor.href);
<a name="line78"></a>  assertEquals(&#39;Should have correct text&#39;, &#39;Text&#39;, link.getCurrentText());
<a name="line79"></a>}
<a name="line80"></a>
<a name="line81"></a>function testSetBoldText() {
<a name="line82"></a>  anchor.innerHTML = &#39;&lt;b&gt;&lt;/b&gt;&#39;;
<a name="line83"></a>  var link = goog.editor.Link.createNewLink(anchor, &#39;http://www.google.com&#39;,
<a name="line84"></a>      &#39;_blank&#39;);
<a name="line85"></a>  assertEquals(&#39;Should be empty&#39;, &#39;&#39;, link.getCurrentText());
<a name="line86"></a>  link.setTextAndUrl(&#39;Text&#39;, &#39;http://docs.google.com/&#39;);
<a name="line87"></a>  assertEquals(&#39;Should point to http://docs.google.com/&#39;,
<a name="line88"></a>      &#39;http://docs.google.com/&#39;, anchor.href);
<a name="line89"></a>  assertEquals(&#39;Should have correct text&#39;, &#39;Text&#39;, link.getCurrentText());
<a name="line90"></a>  assertEquals(&#39;Should still be bold&#39;, goog.dom.TagName.B,
<a name="line91"></a>      anchor.firstChild.tagName);
<a name="line92"></a>}
<a name="line93"></a>
<a name="line94"></a>function testLinkImgTag() {
<a name="line95"></a>  anchor.innerHTML = &#39;&lt;img src=&quot;www.google.com&quot; alt=&quot;alt_txt&quot;&gt;&#39;;
<a name="line96"></a>  var link = goog.editor.Link.createNewLink(anchor, &#39;http://www.google.com&#39;,
<a name="line97"></a>      &#39;_blank&#39;);
<a name="line98"></a>  assertEquals(&#39;Test getCurrentText&#39;, &#39;alt_txt&#39;, link.getCurrentText());
<a name="line99"></a>  link.setTextAndUrl(&#39;newText&#39;, &#39;http://docs.google.com/&#39;);
<a name="line100"></a>  assertEquals(&#39;Test getCurrentText&#39;, &#39;newText&#39;, link.getCurrentText());
<a name="line101"></a>  assertEquals(&#39;Should point to http://docs.google.com/&#39;,
<a name="line102"></a>      &#39;http://docs.google.com/&#39;, anchor.href);
<a name="line103"></a>
<a name="line104"></a>  assertEquals(&#39;Should still have img tag&#39;, goog.dom.TagName.IMG,
<a name="line105"></a>      anchor.firstChild.tagName);
<a name="line106"></a>
<a name="line107"></a>  assertEquals(&#39;Alt should equal &quot;newText&quot;&#39;, &#39;newText&#39;,
<a name="line108"></a>      anchor.firstChild.getAttribute(&#39;alt&#39;));
<a name="line109"></a>}
<a name="line110"></a>
<a name="line111"></a>function testSetMixed() {
<a name="line112"></a>  anchor.innerHTML = &#39;&lt;b&gt;A&lt;/b&gt;B&#39;;
<a name="line113"></a>  var link = goog.editor.Link.createNewLink(anchor, &#39;http://www.google.com&#39;,
<a name="line114"></a>      &#39;_blank&#39;);
<a name="line115"></a>  assertEquals(&#39;Should have text: AB&#39;, &#39;AB&#39;, link.getCurrentText());
<a name="line116"></a>  link.setTextAndUrl(&#39;Text&#39;, &#39;http://docs.google.com/&#39;);
<a name="line117"></a>  assertEquals(&#39;Should point to http://docs.google.com/&#39;,
<a name="line118"></a>      &#39;http://docs.google.com/&#39;, anchor.href);
<a name="line119"></a>  assertEquals(&#39;Should have correct text&#39;, &#39;Text&#39;, link.getCurrentText());
<a name="line120"></a>  assertEquals(&#39;Should not be bold&#39;, goog.dom.NodeType.TEXT,
<a name="line121"></a>      anchor.firstChild.nodeType);
<a name="line122"></a>}
<a name="line123"></a>
<a name="line124"></a>function testPlaceCursorRightOf() {
<a name="line125"></a>  // IE can only do selections properly if the region is editable.
<a name="line126"></a>  var ed = goog.dom.createDom(&#39;div&#39;);
<a name="line127"></a>  goog.dom.replaceNode(ed, anchor);
<a name="line128"></a>  ed.contentEditable = true;
<a name="line129"></a>  ed.appendChild(anchor);
<a name="line130"></a>
<a name="line131"></a>  // In order to test the cursor placement properly, we need to have
<a name="line132"></a>  // link text.  See more details in the test below.
<a name="line133"></a>  anchor.innerHTML = &#39;I am text&#39;;
<a name="line134"></a>
<a name="line135"></a>  var link = goog.editor.Link.createNewLink(anchor, &#39;http://www.google.com&#39;);
<a name="line136"></a>  link.placeCursorRightOf();
<a name="line137"></a>
<a name="line138"></a>  var range = goog.dom.Range.createFromWindow();
<a name="line139"></a>  assertTrue(&#39;Range should be collapsed&#39;, range.isCollapsed());
<a name="line140"></a>  var startNode = range.getStartNode();
<a name="line141"></a>
<a name="line142"></a>  if (goog.userAgent.WEBKIT &amp;&amp; !goog.userAgent.isVersionOrHigher(&#39;528&#39;)) {
<a name="line143"></a>    assertEquals(&#39;Selection should be to the right of the anchor&#39;,
<a name="line144"></a>        anchor, startNode.previousSibling);
<a name="line145"></a>  } else {
<a name="line146"></a>    // Check that the selection is the &quot;right&quot; place.
<a name="line147"></a>    //
<a name="line148"></a>    // If you query the selection, it is actually still inside the anchor,
<a name="line149"></a>    // but if you type, it types outside the anchor.
<a name="line150"></a>    //
<a name="line151"></a>    // Best we can do is test that it is at the end of the anchor text.
<a name="line152"></a>    assertEquals(&#39;Selection should be in anchor text&#39;,
<a name="line153"></a>        anchor.firstChild, startNode);
<a name="line154"></a>    assertEquals(&#39;Selection should be at the end of the text&#39;,
<a name="line155"></a>        anchor.firstChild.length, range.getStartOffset());
<a name="line156"></a>  }
<a name="line157"></a>
<a name="line158"></a>  if (ed) {
<a name="line159"></a>    goog.dom.removeNode(ed);
<a name="line160"></a>  }
<a name="line161"></a>}
<a name="line162"></a>
<a name="line163"></a>function testIsLikelyUrl() {
<a name="line164"></a>  var good = [
<a name="line165"></a>    // Proper URLs
<a name="line166"></a>    &#39;http://google.com&#39;, &#39;http://google.com/&#39;, &#39;http://192.168.1.103&#39;,
<a name="line167"></a>    &#39;http://www.google.com:8083&#39;, &#39;https://antoine&#39;, &#39;https://foo.foo.net&#39;,
<a name="line168"></a>    &#39;ftp://google.com:22/&#39;, &#39;http://user@site.com&#39;,
<a name="line169"></a>    &#39;ftp://user:pass@ftp.site.com&#39;, &#39;http://google.com/search?q=laser%20cats&#39;,
<a name="line170"></a>    &#39;aim:goim?screenname=en2es&#39;, &#39;mailto:x@y.com&#39;,
<a name="line171"></a>
<a name="line172"></a>    // Bad URLs a browser will accept
<a name="line173"></a>    &#39;www.google.com&#39;, &#39;www.amazon.co.uk&#39;, &#39;amazon.co.uk&#39;, &#39;foo2.foo3.com&#39;,
<a name="line174"></a>    &#39;pandora.tv&#39;, &#39;marketing.us&#39;, &#39;del.icio.us&#39;, &#39;bridge-line.com&#39;,
<a name="line175"></a>    &#39;www.frigid.net:80&#39;, &#39;www.google.com?q=foo&#39;, &#39;www.foo.com/j%20.txt&#39;,
<a name="line176"></a>    &#39;foodtv.net&#39;, &#39;google.com&#39;, &#39;slashdot.org&#39;, &#39;192.168.1.1&#39;,
<a name="line177"></a>    &#39;justin.edu?kumar&amp;nbsp;something&#39;, &#39;google.com/search?q=hot%20pockets&#39;,
<a name="line178"></a>
<a name="line179"></a>    // Due to TLD explosion, these could be URLs either now or soon.
<a name="line180"></a>    &#39;ww.jester&#39;, &#39;juicer.fake&#39;, &#39;abs.nonsense.something&#39;, &#39;filename.txt&#39;
<a name="line181"></a>  ];
<a name="line182"></a>  for (var i = 0; i &lt; good.length; i++) {
<a name="line183"></a>    assertTrue(good[i] + &#39; should be good&#39;,
<a name="line184"></a>        goog.editor.Link.isLikelyUrl(good[i]));
<a name="line185"></a>  }
<a name="line186"></a>
<a name="line187"></a>  var bad = [
<a name="line188"></a>    // Definitely not URLs
<a name="line189"></a>    &#39;bananas&#39;, &#39;http google com&#39;, &#39;&lt;img&gt;&#39;, &#39;Sad :/&#39;, &#39;*garbage!.123&#39;,
<a name="line190"></a>    &#39;ftp&#39;, &#39;http&#39;, &#39;/&#39;, &#39;https&#39;, &#39;this is&#39;, &#39;*!&amp;.banana!*&amp;!&#39;,
<a name="line191"></a>    &#39;www.jester is gone.com&#39;, &#39;ftp .nospaces.net&#39;, &#39;www_foo_net&#39;,
<a name="line192"></a>    &quot;www.&#39;jester&#39;.net&quot;, &#39;www:8080&#39;,
<a name="line193"></a>    &#39;www . notnsense.com&#39;, &#39;email@address.com&#39;,
<a name="line194"></a>
<a name="line195"></a>    // URL-ish but not quite
<a name="line196"></a>    &#39;  http://www.google.com&#39;, &#39;http://www.google.com:8081   &#39;,
<a name="line197"></a>    &#39;www.google.com foo bar&#39;, &#39;google.com/search?q=not quite&#39;
<a name="line198"></a>  ];
<a name="line199"></a>
<a name="line200"></a>  for (i = 0; i &lt; bad.length; i++) {
<a name="line201"></a>    assertFalse(bad[i] + &#39; should be bad&#39;,
<a name="line202"></a>        goog.editor.Link.isLikelyUrl(bad[i]));
<a name="line203"></a>  }
<a name="line204"></a>}
<a name="line205"></a>
<a name="line206"></a>function testIsLikelyEmailAddress() {
<a name="line207"></a>  var good = [
<a name="line208"></a>    // Valid email addresses
<a name="line209"></a>    &#39;foo@foo.com&#39;, &#39;foo1@foo2.foo3.com&#39;, &#39;f45_1@goog13.org&#39;, &#39;user@gmail.co.uk&#39;,
<a name="line210"></a>    &#39;jon-smith@crazy.net&#39;, &#39;roland1@capuchino.gov&#39;, &#39;ernir@gshi.nl&#39;,
<a name="line211"></a>    &#39;JOON@jno.COM&#39;, &#39;media@meDIa.fREnology.FR&#39;, &#39;john.mail4me@del.icio.us&#39;,
<a name="line212"></a>    &#39;www9@wc3.madeup1.org&#39;, &#39;hi@192.168.1.103&#39;, &#39;hi@192.168.1.1&#39;
<a name="line213"></a>  ];
<a name="line214"></a>  for (var i = 0; i &lt; good.length; i++) {
<a name="line215"></a>    assertTrue(goog.editor.Link.isLikelyEmailAddress(good[i]));
<a name="line216"></a>  }
<a name="line217"></a>
<a name="line218"></a>  var bad = [
<a name="line219"></a>    // Malformed/incomplete email addresses
<a name="line220"></a>    &#39;user&#39;, &#39;@gmail.com&#39;, &#39;user@gmail&#39;, &#39;user@.com&#39;, &#39;user@gmail.c&#39;,
<a name="line221"></a>    &#39;user@gmail.co.u&#39;, &#39;@ya.com&#39;, &#39;.@hi3.nl&#39;, &#39;jim.com&#39;,
<a name="line222"></a>    &#39;ed:@gmail.com&#39;, &#39;*!&amp;.banana!*&amp;!&#39;, &#39;:jon@gmail.com&#39;,
<a name="line223"></a>    &#39;3g?@bil.com&#39;, &#39;adam be@hi.net&#39;, &#39;john\nsmith@test.com&#39;,
<a name="line224"></a>    &quot;www.&#39;jester&#39;.net&quot;, &quot;&#39;james&#39;@covald.net&quot;, &#39;ftp://user@site.com/&#39;,
<a name="line225"></a>    &#39;aim:goim?screenname=en2es&#39;, &#39;user:pass@site.com&#39;, &#39;user@site.com yay&#39;
<a name="line226"></a>  ];
<a name="line227"></a>
<a name="line228"></a>  for (i = 0; i &lt; bad.length; i++) {
<a name="line229"></a>    assertFalse(goog.editor.Link.isLikelyEmailAddress(bad[i]));
<a name="line230"></a>  }
<a name="line231"></a>}
<a name="line232"></a>
<a name="line233"></a>function testIsMailToLink() {
<a name="line234"></a>  assertFalse(goog.editor.Link.isMailto());
<a name="line235"></a>  assertFalse(goog.editor.Link.isMailto(null));
<a name="line236"></a>  assertFalse(goog.editor.Link.isMailto(&#39;&#39;));
<a name="line237"></a>  assertFalse(goog.editor.Link.isMailto(&#39;http://foo.com&#39;));
<a name="line238"></a>  assertFalse(goog.editor.Link.isMailto(&#39;http://mailto:80&#39;));
<a name="line239"></a>
<a name="line240"></a>  assertTrue(goog.editor.Link.isMailto(&#39;mailto:&#39;));
<a name="line241"></a>  assertTrue(goog.editor.Link.isMailto(&#39;mailto://&#39;));
<a name="line242"></a>  assertTrue(goog.editor.Link.isMailto(&#39;mailto://ptucker@gmail.com&#39;));
<a name="line243"></a>}
<a name="line244"></a>
<a name="line245"></a>function testGetValidLinkFromText() {
<a name="line246"></a>  var textLinkPairs = [
<a name="line247"></a>    // input text, expected link output
<a name="line248"></a>    &#39;www.foo.com&#39;, &#39;http://www.foo.com&#39;,
<a name="line249"></a>    &#39;user@gmail.com&#39;, &#39;mailto:user@gmail.com&#39;,
<a name="line250"></a>    &#39;http://www.foo.com&#39;, &#39;http://www.foo.com&#39;,
<a name="line251"></a>    &#39;https://this.that.edu&#39;, &#39;https://this.that.edu&#39;,
<a name="line252"></a>    &#39;nothing to see here&#39;, null
<a name="line253"></a>  ];
<a name="line254"></a>  var link = new goog.editor.Link(anchor, true);
<a name="line255"></a>
<a name="line256"></a>  for (var i = 0; i &lt; textLinkPairs.length; i += 2) {
<a name="line257"></a>    link.currentText_ = textLinkPairs[i];
<a name="line258"></a>    var result = link.getValidLinkFromText();
<a name="line259"></a>    assertEquals(textLinkPairs[i + 1], result);
<a name="line260"></a>  }
<a name="line261"></a>}
</pre>


</body>
</html>
